
install.packages("gplots")

library(foreign)
library(gplots)
library(plyr)


## defining functions

add_legend <- function(...) {
  opar <- par(fig=c(0, 1, 0, 1), oma=c(0, 0, 0, 0), 
              mar=c(0.5, 0.5, 0.5, 0.5), new=TRUE)
  on.exit(par(opar))
  plot(0, 0, type='n', bty='n', xaxt='n', yaxt='n')
  legend(...)
}


setwd("~/Dropbox/RadicalRight_HU/Identificaion_JOP_2016/jop_final/Data_folder")


df<-read.dta("figure_1_workfile.dta")

names(df)

means <- ddply(.data = df, .variables= .(cat), 
             .fun = summarise, 
             wf = weighted.mean(x=a_spendmore,  w=suly, na.rm=T),
             disc = weighted.mean(x=a_excludebars,  w=suly, na.rm=T),
             seg= weighted.mean(x=a_segregate,  w=suly, na.rm=T),
             threat= weighted.mean(x=a_demogthreat,  w=suly, na.rm=T),
             kids= weighted.mean(x=a_childforwel,  w=suly, na.rm=T),
             crime= weighted.mean(x=a_criminals,  w=suly, na.rm=T))


m<-as.matrix(means)[2:4,2:7]

class(m) <- "numeric"
m<-100*m




items<-c("Welfare spending on the \nRoma should be increased", "Segregation in bars \nshould be allowed",
         "The Roma should be \nsegregated from the majority", "The Roma population \ngrowth is threatening",
         "The Roma raise children \nto get more welfare", "The Roma are \nnatural born criminals")


cairo_pdf(file = "figure1.pdf", width=8, height=6)

par(mfrow=c(2,3))
par(mar=c(6.5, 1, 5,1))
for (i in 1:6) {
  barplot2(m[,i], beside=T, horiz=T,  las=1, space=c(.2,.5),  xlim=c(-2,max(m[,i])+2), 
          col=c("white", "grey70", "black"), , plot.grid=T, cex.main=1.7, main=items[i])
  if (i==2) {
    title(xlab="Proportion agreeing (%)",line=4, cex.lab=1.7)
    
  }
  box() 
}


add_legend("bottom", c("Roma descent, Roma ID", "Roma descent, no Roma ID", "No Roma descent, no Roma ID"), bty="n",
           fill=c("black", "grey70", "white"),horiz=T,cex=1.4)

dev.off()


